import 'package:flutter/widgets.dart';

enum ViewState { Loading, Success, Error, Empty }

class BaseViewModel extends ChangeNotifier {
  String errorMsg;

  ViewState _state = ViewState.Success;

  ViewState get state => _state;

  void setState(ViewState viewState, {bool rebuild = true}) {
    _state = viewState;
    if (rebuild) notifyListeners();
  }

  void setFailed(String msg, {bool rebuild = true}) {
    _state = ViewState.Error;
    errorMsg = msg;
    if (rebuild) notifyListeners();
  }

  void onReTry() {}

  bool _hasDispose = false;

  @override
  void notifyListeners() {
    if (_hasDispose) return;
    super.notifyListeners();
  }

  @override
  void dispose() {
    _hasDispose = true;
    super.dispose();
  }
}
